package com.shd.day03.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author S5343
 * @date 2018年7月22日下午2:58:45
 */
public class SimpleStream {

	public static void main(String[] args) {
		// hava a dish list
		List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
				new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
				new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER),
				new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
				new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
		List<String> names = getDishNameByCollections(menu);
		List<String> names1 = getDishNameByStream(menu);

		List<String> result = menu.stream().filter(d -> {
			System.out.println("filter->" + d.getName());
			return d.getCalories() > 300;
		}).map(d -> {
			System.out.println("map->" + d.getName());
			return d.getName();
		}).collect(Collectors.toList());

		System.out.println(result);
	}

	private static List<String> getDishNameByStream(List<Dish> menu) {
		return menu.stream().filter(d -> d.getCalories() < 400).sorted(Comparator.comparing(Dish::getCalories))
				.map(Dish::getName).collect(Collectors.toList());
	}

	private static List<String> getDishNameByCollections(List<Dish> menu) {
		List<Dish> lowCalories = new ArrayList<>();
		// filter and get Calories less 400
		for (Dish dish : menu) {
			if (dish.getCalories() < 400) {
				lowCalories.add(dish);
			}
		}
		// sort
		Collections.sort(lowCalories, (d1, d2) -> Integer.compare(d1.getCalories(), d2.getCalories()));
		List<String> names = new ArrayList<>();
		for (Dish dish : lowCalories) {
			names.add(dish.getName());
		}
		return names;
	}

}
